 <!DOCTYPE html>

<html>
	<head>
	
	</head>
	<body>
<?php
	include_once "include/Functions.php";

	$exercise_id= $_GET['exercise_id'];
	$person_id = $_GET['person_id'];

	$sql = "SELECT  DISTINCT lat, lon, d_time 
				FROM `data_exerc` 
				WHERE id_exerc = $exercise_id
				AND id_person = $person_id 
				ORDER BY d_time;";
	$mat = Functions::getDataSql($sql);
	
	$cont= count($mat);
	
	//1 point per 5 seconds
	/*$salt = 1; //incremento
	$duration = $ex['duration']/1000;
	
	if( $cont > $duration/5)
		$salt = (int) ceil($cont / ($duration/5));
		*/
	echo " <script type='text/javascript'>	";
	
	echo "  var quant = {$cont}; 
			var exercise_id = {$exercise_id}; 
			var person_id = {$person_id}; 
			var points_chart = []; 
			var points_alt = []; 
			var points_new = []; 
			url_chart = 'http://open.mapquestapi.com/elevation/v1/chart?key=Fmjtd%7Cluurn90r2g%2C8a%3Do5-9wtndu&inFormat=kvp&shapeFormat=cmp&width=700&height=500&latLngCollection='; 
			url_raw  =  'http://open.mapquestapi.com/elevation/v1/profile?key=Fmjtd%7Cluurn90r2g%2C8a%3Do5-9wtndu&callback=handleResponse&shapeFormat=cmp&latLngCollection='; ";
	
	for( $i=0; $i < $cont; $i++ ) {
		echo " points_alt.push({$mat[$i]['lat']}); 
			   points_alt.push({$mat[$i]['lon']}); 
			   var aux= {}; 
			   aux.d_time = {$mat[$i]['d_time']}; 
			   aux.alt= 0; 
			   points_new.push(aux); ";
			   
		//if( $i % $salt == 0)
			echo " points_chart.push({$mat[$i]['lat']});
				   points_chart.push({$mat[$i]['lon']}); ";		
	}	
	
	echo "</script>";
?>
        <script src="../js/jquery-1.11.0.js"></script>
		
<script type="text/javascript">
	function compress(points, precision) {
		   var oldLat = 0, oldLng = 0, len = points.length, index = 0;
		   var encoded = '';
		   precision = Math.pow(10, precision);
		   while (index < len) {
			  //  Round to N decimal places
			  var lat = Math.round(points[index++] * precision);
			  var lng = Math.round(points[index++] * precision);

			  //  Encode the differences between the points
			  encoded += encodeNumber(lat - oldLat);
			  encoded += encodeNumber(lng - oldLng);
			  
			  oldLat = lat;
			  oldLng = lng;
		   }
		   return encoded;
		}

	function encodeNumber(num) {
		   var num = num << 1;
		   if (num < 0) {
			  num = ~(num);
		   }
		   var encoded = '';
		   while (num >= 0x20) {
			  encoded += String.fromCharCode((0x20 | (num & 0x1f)) + 63);
			  num >>= 5;
		   }
		   encoded += String.fromCharCode(num + 63);
		   return encoded;   
	}

	//url_chart += compress(points_chart, 5);
	url_raw += compress(points_alt, 5);
	
	
	function handleResponse(response) {
		var array_data = response.elevationProfile;
		var i = 0;
		var json_array = [];
				
		//Se a quantidade retornada de dados for igual a quantidade requisitada
		if( quant == array_data.length){
			json_array.push(exercise_id);
			json_array.push(person_id);
			json_array.push(quant);
		
			for(; i < array_data.length; i++){
				points_new[i].alt= array_data[i].height;
				points_new[i].distance = array_data[i].distance;
			}
			
			json_array.push(points_new);
			$.ajax({
				type: "POST",
				contentType: "application/json; charset=utf-8",
				url: "pages/update_alt.php",
				dataType: 'json',
				data: JSON.stringify(json_array)
			});
		}
	}	
	
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url_raw;
    document.body.appendChild(script);	
	
</script>
</body>
</html>